
package com.csc440.team2.database.entities;

import java.io.Serializable;
import java.util.List;


import android.content.Context;


import com.csc440.team2.database.DBAdapter;


/**
 * Resource implementation for a SMIL Message
 * 
 * @author Jeremy Spitzig
 */
public class Resource implements Serializable
{
    private static final long serialVersionUID = 1L;
    private long id;
    private long messageId;
    private ResourceType resourceType;
    private String location;


    public Resource()
    {
        setId(-1);
        setMessageId(-1);
        setResourceType(ResourceType.IMAGE);
        setLocation("");
    }


    public Resource(long id)
    {
        setId(id);
    }


    public Resource(long messageID, ResourceType resourceType, String location)
    {
        setId(-1);
        setMessageId(messageID);
        setResourceType(resourceType);
        setLocation(location);
    }


    public Resource(long id, long messageID, ResourceType resourceType, String location)
    {
        setId(id);
        setMessageId(messageID);
        setResourceType(resourceType);
        setLocation(location);
    }


    /**
     * @return the id
     */
    public long getId()
    {
        return id;
    }


    /**
     * @param id the id to set
     */
    public void setId(long id)
    {
        this.id = id;
    }


    /**
     * @return the messageID
     */
    public long getMessageId()
    {
        return messageId;
    }


    /**
     * @param messageID the messageID to set
     */
    public void setMessageId(long messageId)
    {
        this.messageId = messageId;
    }


    /**
     * @return the resourceType
     */
    public ResourceType getResourceType()
    {
        return resourceType;
    }


    /**
     * @param resourceType the resourceType to set
     */
    public void setResourceType(ResourceType resourceType)
    {
        this.resourceType = resourceType;
    }


    /**
     * @return the location
     */
    public String getLocation()
    {
        return location;
    }


    /**
     * @param location the location to set
     */
    public void setLocation(String location)
    {
        this.location = location;
    }


    /**
     * Gets a reference to the database adapter
     * 
     * @param context activity context
     * @return database adapter
     */
    private static DBAdapter getDatabase(Context context)
    {
        DBAdapter adapter = new DBAdapter(context);
        adapter.open();
        return adapter;
    }


    /**
     * Loads this resource from the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean load(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getResource(this);
        adapter.close();
        return result;
    }


    /**
     * Save this resource to the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean save(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.saveResource(this);
        adapter.close();
        return result;
    }


    /**
     * Deletes this resource from the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean delete(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.deleteResource(this);
        adapter.close();
        return result;
    }


    /**
     * Gets a list of all resources in the datastore
     * 
     * @param context activity context
     * @return list of resources
     */
    public static List<Resource> getAll(Context context)
    {
        List<Resource> result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getAllResources();
        adapter.close();
        return result;
    }


    /**
     * Gets a list of all resources in the datastore
     * 
     * @param context activity context
     * @return list of resources
     */
    public static List<Resource> getAllByMessageID(Context context, long messageID)
    {
        List<Resource> result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getResourcesByMessageId(messageID);
        adapter.close();
        return result;
    }
}
